__all__ = ['Serie', 'Season', 'Episode']

from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey
from sqlalchemy.orm import relation, backref, object_session, mapper

import validator

#meta data
metadata = MetaData()

###TABLES###
seriesTable = Table('tbSeries', metadata,
                    Column('id', Integer, primary_key=True),
                    Column('name', String, nullable=False),
                    Column('feed_url', String))

seasonsTable = Table('tbSeasons', metadata,
                     Column('id', Integer, primary_key=True),
                     Column('serie_id', Integer, ForeignKey("tbSeries.id"), nullable=False),
                     Column('name', String, nullable=False),
                     Column('active', Integer, nullable=False))

episodesTable = Table("tbEpisodes", metadata,
                      Column('id', Integer, primary_key=True),
                      Column('season_id', Integer, ForeignKey('tbSeasons.id'), nullable=False),
                      Column('name', String, nullable=False),
                      Column('description', String, nullable=False),
                      Column('status', String, nullable=False),
                      Column('torrent_id', Integer))
###########

###OBJECTS###
class Base(object):
    def session(self):
        return object_session(self)

class Serie(Base):    
    #construtor
    def __init__(self, name, feed):
        self.name = name
        self.feed_url = feed
        
    def __repr__(self):
        return "<Serie('%s', '%s', '%s')>" % (self.id, self.name, self.feed_url)
        
class Season(Base):
    #construtor
    def __init__(self, name, active):
        self.name = name
        self.active = active
        
    def __repr__(self):
        return "<Season('%s', '%s', '%s', '%s')>" % (self.id, self.serie_id, self.name, self.active)
        
class Episode(Base):
    #construtor
    def __init__(self, name, desc, status, torId):
        self.name = name
        self.description = desc
        self.status = status
        self.torrent_id = torId
    
    def __repr__(self):
        return "<Episode('%s', '%s', '%s', '%s', '%s', '%s')>" % (self.id, self.season_id, self.name, self.description, self.status, self.torrent_id)
#############


###Table -> Object Maps###
mapper(Serie, seriesTable)
mapper(Season, seasonsTable, properties={'serie': relation(Serie, backref=backref('seasons', lazy='dynamic'))})
mapper(Episode, episodesTable, properties={'season': relation(Season, backref=backref('episodes', lazy='dynamic'))})
##########################
